<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>编辑地址</title>
    <meta name="keywords" content="编辑地址">
    <meta name="description" content="">
    <link rel="stylesheet" href="{php echo $this->cur_mobile_path}/css/weui.css?Dj8">
    <link rel="stylesheet" href="{php echo $this->cur_mobile_path}/css/custom.css?Dj8345">
    <script src="{php echo $this->cur_mobile_path}/script/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script src="{php echo $this->cur_mobile_path}/script/jquery-weui.js?23Dj8" type="text/javascript" charset="UTF-8"></script>
    <style>
        .weui-dialog__btn, .weui-navbar__item.weui_bar__item_on, .color-strong, .chip-row .toutiao, .weui-cells_checkbox .weui-check:checked + .weui-icon-checked:before, .weui-vcode-btn, .weui-dialog__btn_primary, .weui-cells_radio .weui-check:checked + .weui-icon-checked:before, .weui-icon-checked:before, .weui-agree__checkbox:checked:before, .weui-icon-success-circle, .weui-icon-success-no-circle, .weui-search-bar__cancel-btn, .weui-tabbar__item.weui-bar__item_on .weui-tabbar__icon, .weui-tabbar__item.weui-bar__item_on .weui-tabbar__icon > i, .weui-tabbar__item.weui-bar__item_on .weui-tabbar__label, .main_color, .weui-tabbar__item.weui-bar__item--on .weui-tabbar__label, .picker-button, .weui-form-preview__btn_primary {
            color: {$setting['style_base']} !important
        }
        .weui_bar__item_on span:after, .weui-btn_primary, .weui-btn_primary:not(.weui-btn_disabled):active, .weui-btn_mini, .x_header, .main_bg, .position li.current, .position1 li.current, .post-tags .tag-on.weui-btn_default, .is-green, .weui-slider__track, .tsbtn_m {
            background-color: {$setting['style_base']} !important
        }
        .weui-navbar.filter_top .weui_bar__item_on i.icon-xiangxia, .weui-navbar.fix_float .weui_bar__item_on i.icon-xiangxia {
            border-color: {$setting['style_base']} transparent transparent transparent
        }
        .fzopen {
            text-shadow: 1px 1px 5px rgba(0, 0, 0, 0.8)
        }
        .mod-feed-text.is-three {
            -webkit-line-clamp: 4
        }</style>
    <script>var IN_WECHAT = '1';</script>
</head>
<body>
<link rel="stylesheet" href="{php echo $this->cur_mobile_path}/css/hp.css?Dj8"/>
<style>.weui-switch-cp__input:checked ~ .weui-switch-cp__box, .weui-switch:checked, .float_btn {
    background-color: {$setting['style_base']} !important;
    border-color: {$setting['style_base']} !important
}

.light_box a.main_color:after, .index_bd, .tsbtn, .tsbtn_mini {
    background: {$setting['style_base']}
}

.main_border {;
    border-color: {$setting['style_base']} !important
}

.lq_mine {
    background: rgba(0, 194, 196, 0.05);
    border: 1px solid {$setting['style_base']}
}

.tiing {
    background-color: {$setting['style_base']}
}
.mpc_body {
    background-image: url("{php echo $this->cur_mobile_path}/image/mpbg1.png")
}</style>
<div class="page__bd">
    <header class="x_header bgcolor_11 cl f15">
        <a class="z f14" href="javascript:window.history.go(-1);"><i class="iconfont icon-fanhuijiantou w15"></i>返回</a>
        <a class="y sidectrl " href="{php echo $this->createMobileUrl('usercenter', array('sid' => $_GPC['sid']), true)}">我的</a>    <div class="navtitle">个人设置</div></header>
    <div class="x_header_fix"></div>
    {if $operation == 'display'}
    <div>
        <div class="weui-cells__title">收货地址管理</div>
        <div id="loading-show" class="weui-loadmore hidden">
            <i class="weui-loading"></i>
            <span class="weui-loadmore__tips">正在加载</span>
        </div>
        {if empty($list)}
        <div id="loading-none" class="weui-loadmore weui-loadmore_line">
            <div class="hs_empty"><i class="icon iconfont icon-zanwuwenda"></i><p>没有更多了</p></div>
        </div>
        {else}
        {loop $list $row}
        <div class="weui-form-preview " id="li_{$row['id']}">
            <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item tl">
                    <span class="f24 c6">{$row['realname']} <em class="f15 main_color">{$row['mobile']}</em>
                    {if $row['isdefault']==1}
                    <em class="f15 y c9">默认收货地址</em>
                    {/if}
                    </span>
                    <span class="weui-form-preview__value f15">{$row['address']} {$row['doorplate']}</span>
                </div>
            </div>
            <div class="weui-form-preview__ft">
                {php $id = $row['id'];}
                {php $url = $this->createMobileUrl('useraddress', array('storeid' => $storeid, 'op' =>'delete', 'id'=> $id),true)}
                <a class="weui-form-preview__btn weui-form-preview__btn_default f17" onclick="return confirm_del2('确定删除?', '{$url}', {$id});" href="javascript:">删除</a>
                {php $updateurl = $this->createMobileUrl('useraddress', array('storeid' => $storeid, 'mode' => $mode, 'tablesid' => $tablesid, 'op' => 'post', 'id' => $row['id'], 'selectdate' => $selectdate, 'timeid' => $timeid, 'rtype' => $rtype), true);}

                <button type="submit" class="weui-form-preview__btn weui-form-preview__btn_primary f17" href="javascript:" onclick="location.href='{$updateurl}'">修改</button>
                <a onclick="setdefault({$row['id']});" class="weui-form-preview__btn weui-form-preview__btn_primary f17">选择</a>            </div>
        </div>
        {/loop}
        {/if}
    </div>
    <div class="fix-bottom">
        <a class="weui-btn weui-btn_primary" data-dft="1" onclick="location.href='{$addurl}'">新增地址</a>
    </div>
    {else}
        <input type="hidden" name="lat" id="lat" value="{$item['lat']}">
        <input type="hidden" name="lng" id="lng" value="{$item['lng']}">
        <div class="weui-cells  ">
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">联系人<em class="color-red">*</em></label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" type="text" placeholder="请填写姓名" id="realname" name="realname" value="{$item['realname']}">
                </div>
            </div>

            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">联系电话<em class="color-red">*</em></label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" type="tel" placeholder="请填写手机号" value="{$item['mobile']}"
                           id="mobile"
                           name="mobile">
                </div>
            </div>
            <div class="weui-cell weui-cell_vcode">
                <div class="weui-cell__hd">
                    <label class="weui-label">地址<em class="color-red">*</em></label>
                </div>
                <div class="weui-cell__bd enter_addr">
                    <input class="weui-input" id="mapaddress" name="mapaddress" placeholder="请填写地址或定位" type="text"
                           value="{$item['address']}">
                </div>
                <div class="weui-cell__ft">
                    <button class="weui-vcode-btn" id="openlocation" type="button">定位</button>
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">门牌号<em class="color-red">*</em></label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" type="text" placeholder="楼层/门牌号等信息" id="doorplate" name="doorplate"
                           value="{$item['doorplate']}">
                </div>
            </div>
        </div>
        <div class="weui-cells weui-cells_form" style="margin-top:10px">
            <div class="weui-cell weui-cell_switch">
                <div class="weui-cell__bd">设为默认收货地址</div>
                <div class="weui-cell__ft">
                    <input hidden="hidden" name="btn1" id="btn" value="{if $item['isdefault']==1}on{else}off{/if}"
                           type="radio" checked="checked" >
                    <input class="weui-switch" name="isdefault" value="1" type="checkbox" {if $item['isdefault']==1}checked{/if}>
                </div>
            </div>
        </div>
        <div class="fix-bottom mt10" style="position:relative">
            <input type="button" class="weui-btn weui-btn_primary" name="dosubmit" id="dosubmit" value="保存" onclick="postmain();">
        </div>
    {/if}
    <div class="footer_fix"></div>
</div>

<div id="mapouter" style="z-index:999" class="weui-popup__container">
    <div class="weui-popup__modal">
        <div id="mapcontainer"></div>
        <div class="fix-bottom">
            <div class="weui-flex">
                <a class="mt0 half weui-btn weui-btn_default close-popup" href="javascript:;">关闭</a>
                <a class="mt0 ml15 half weui-btn weui-btn_primary confirm-popup" href="javascript:;">确定</a>
            </div>
        </div>
    </div>
</div>
<div id="popctrl" class="weui-popup__container" style="z-index:1001">
    <div class="weui-popup__modal">
        <div style="height: 100vh"><img id="photo"></div>
        <div class="pub_funcbar">
            <a class="weui-btn close-popup weui-btn_primary" data-method="confirm">确定</a>
            <a class="weui-btn close-popup weui-btn_default" data-method="destroy">取消</a>
        </div>
    </div>
</div>
<div class="masker" onclick='$(".choose_ctrl").select("close")'></div>

<script src="https://map.qq.com/api/js?v=2.exp&key={$setting['qqmapapi']}" type="text/javascript"></script>
<script src="{php echo $this->cur_mobile_path}/script/geolocation.js?Dj8" type="text/javascript"></script>
<div class="cl footer_fix"></div>
<div id="backtotop" class="backtotop"><span class="icon-vertical-align-top"><i
        class="iconfont icon-iconfontarrowup"></i></span></div>
<script>
    $(function () {
        $("#ground").bind("click", function () {
            if($("#btn").val()=="off"){
                $("#btn").val("on");
                console.log("本校");
            }else{
                $("#btn").val("off");
                console.log("全国");
            }
        });
    });


    function confirm_del2(word, url, id) {
        $.confirm(word, function () {
            $.showLoading();

            $.ajax({
                type: 'post',
                url: url, dataType: 'json', success: function (data) {
                    $('#li_' + id).remove();
                    $.hideLoading();
                }, error: function () {
                    $.hideLoading();
                }
            });
        }, function () {
        });
        return false;
    }

    function setdefault(id) {
        if (true) {
            var url = "{php echo $this->createMobileUrl('useraddress', array('op' => 'setdefault'), true)}";
            $.ajax({
                url: url, type: "post", dataType: "json", timeout: "10000",
                data: {
                    id:id
                },
                success: function (data) {
                    "{if $mode==0}"
                    location.reload();
                    return false;
                    "{elseif $mode==3}"
                    var url = "{php echo $this->createMobileUrl('reservationdetail', array('storeid' => $storeid, 'mode' => $mode, 'tablesid' => $tablesid, 'selectdate' => $selectdate, 'timeid' => $timeid, 'rtype' => $rtype), true)}" + "&addressid=" + id;
                    "{else}"
                    var url = "{php echo $this->createMobileUrl('wapmenu', array('storeid' => $storeid, 'mode' => $mode, 'tablesid' => $tablesid), true)}" + "&addressid=" + id;
                    "{/if}"
                    location.href = url;
                },error: function () {
                    alert("网络延迟！");
                }
            });
        }
    }

    function postmain() {
        if (true) {
            var url = "{php echo $this->createMobileUrl('useraddress', array('storeid' => $storeid, 'id' => $id, 'op' => 'AddAddress'), true)}";
            var address = $("#mapaddress").val();
            var doorplate = $("#doorplate").val();
            var realname = $("#realname").val();
            var mobile = $("#mobile").val();
            var lat = $("#lat").val();
            var lng = $("#lng").val();
            var isdefault = 0;
            if($("#btn").val()=="on"){
                isdefault = 1;
            }

            if(!doorplate){
                $.alert("请输入您的楼层/门牌号等信息");
                return false;
            }
            if(!realname){
                $.alert("请输入您的姓名");
                return false;
            }
            if (!checkMobile($("#mobile").val())) {
                return false;
            }
//            $.alert(isdefault);
            $.ajax({
                url: url, type: "post", dataType: "json", timeout: "10000",
                data: {
                    "lat":lat,
                    "lng":lng,
                    "address": address,
                    "doorplate":doorplate,
                    "realname":realname,
                    "mobile":mobile,
                    "isdefault":isdefault
                },
                success: function (data) {
                    var url = "{php echo $this->createMobileUrl('useraddress', array('storeid' => $storeid, 'mode' => $mode, 'tablesid' => $tablesid, 'selectdate' => $selectdate, 'timeid' => $timeid, 'rtype' => $rtype), true)}" + "";
                    location.href = url;
                },error: function () {
                    alert("网络异常！");
                }
            });
        }
    }

    function checkMobile($mobileVal) {
        if (checkEmpty($mobileVal) == false) {
            $.alert("请输入您的联系电话");
            return false;
        } else {
            if ($mobileVal.length != 11) {
                $.alert("手机号码长度不正确");
                return false;
            }
            else if (/^(((13[0-9]{1})|15[0-9]{1}|16[0-9]{1}|17[0-9]{1}|18[0-9]{1}|19[0-9]{1}|14[0-9]{1})+\d{8})$/.test($mobileVal) == false) {
                $.alert("手机号码格式不正确");
                return false;
            }
            else {
                return true;
            }
        }
    }

    //非空校验
    function checkEmpty(param) {
        if (param == "" || param == null || param == undefined) {
            return false;
        } else {
            return true;
        }
    }
</script>
<div class="mask none"></div>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>


<script src="{php echo $this->cur_mobile_path}/script/clipboard.min.js?Dj8" type="text/javascript"></script>
<script>
    var clipboard = new ClipboardJS('.fzbtn');
    clipboard.on('success', function (e) {
        $.alert('微信号<br> ' + e.text + ' <br> 复制成功' + ' <br> 快到微信中搜索添加吧！');
    });
    function lxfs_tip(obj, mobile, weixin) {
        var that = $(obj);
        var lxfsbox = $('#lxfs');
        if (!mobile) {
            lxfsbox.find('#lxfs_mobile0').hide();
        } else {
            lxfsbox.find('#lxfs_mobile0').show();
            lxfsbox.find('#lxfs_mobile').html(mobile);
            lxfsbox.find('#lxfs_mobile1').attr('href', 'tel:' + mobile);
        }
        lxfsbox.find('#lxfs_weixin').html(weixin);
        lxfsbox.find('#lxfs_weixin1').attr('data-clipboard-text', weixin);
        var htm = lxfsbox.html();
        $.alert(htm);
    }
    function jumpDownload() {
        $.confirm("", function () {
            window.location.href = '';
        }, function () {
        });
        return false;
    }
</script>
</body>
</html>
{php echo register_jssdk(false);}
<script>var HB_INWECHAT = '{php echo $this->isWeixin();}', mkey = "{$setting['qqmapapi']}", HS_MULTIUPLOAD = "1";</script>
<script>
    window.addEventListener('message', function(event) {
        // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
        var loc = event.data;
        if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
            console.log('location', loc);
        }
    }, false);
</script>
<script>

    var chooseMapRes = [], OBJ = {};
    function setForm(lat, lng, deft) {
        $.showLoading();
        var url ="{php echo $this->createMobileurl('getloc', array(), true)}" + '&lat=' + lat + '&lng=' + lng +
                '&inajax=1';
        $.ajax({
            type: 'GET',
            url: url,
            dataType: 'xml',
            success: function (data) {
                $.hideLoading();
                if (null == data) {
                    tip_common('error|' + ERROR_TIP);
                    return false;
                }
                var s = data.lastChild.firstChild.nodeValue;
                if (s.indexOf('error') != -1) {
                    tip_common(s);
                } else {
                    var _actions = [];
                    OBJ = jQuery.parseJSON(s.split('|')[1]);
                    if (deft) {
                        setFormField(OBJ[0]);
                        return true;
                    }
                    for (var j in OBJ) {
                        _actions.push({
                            text: OBJ[j].address,
                            className: 'obj_ obj_' + j,
                            onClick: function () {
                                $.closePopup();
                            }
                        });
                    }
                    $.actions({actions: _actions});
                }
            },
            error: function () {
                $.hideLoading();
            }
        });
    }
    function setFormField(subj) {
        $('#mapaddress').val((subj.addr || subj.address));
        $("#lat").val(subj.location.lat);
        $("#lng").val(subj.location.lng);

//        $("input[name='form[addr]']").val(subj.address);
//        $("input[name='form[lat]']").val(subj.location.lat);
//        $("input[name='form[lng]']").val(subj.location.lng);
//        $("input[name='form[province]']").val(subj.address_component.province);
//        $("input[name='form[city]']").val(subj.address_component.city);
//        $("input[name='form[district]']").val(subj.address_component.district);
//        $("input[name='form[street]']").val(subj.address_component.street);
//        $("input[name='form[street_number]']").val(subj.address_component.street_number);
    }

    function setPoint(position) {
//        console.log(position);

//        if (typeof position.type != 'undefined') {
//            if (position.type == 'ip') {
//                if (IGNORETIP) {
//                } else {
//                    $.alert('无法定位当前点，请地图选点' + position.type);
//                    chooseMap(position);
//                }
//                return false;
//            }
//        }

//        $('#mapaddress').val((position.addr || position.address));
        setForm((position.latitude || position.lat), (position.longitude || position.lng), 1);
    }
    function chooseMap(position) {
        var center = new qq.maps.LatLng((position.latitude || position.lat), (position.longitude || position.lng));
        var mapinit = function () {
            geocoder = new qq.maps.Geocoder({
                complete: function (result) {
                    chooseMapRes = result;
                    console.log(result);
                }
            });
            geocoder.getAddress(center);
            map = new qq.maps.Map(document.getElementById("mapcontainer"), {center: center, zoom: 13});
            marker = new qq.maps.Marker({
                position: center, map: map
            });
            qq.maps.event.addListener(map, 'click', function (event) {
                var tmpcenter = new qq.maps.LatLng(event.latLng.getLat(), event.latLng.getLng());
                marker.setPosition(tmpcenter);
                geocoder.getAddress(tmpcenter);
            });
            $("#mapouter").popup();
        };
        mapinit();
    }
    $(document).on('click', '.obj_', function () {
        var k = parseInt($(this)[0].classList[2].replace('obj_', ''));
        setFormField(OBJ[k]);
        $("#new_popup").popup();
    });

    $('#openlocation').on('click', function () {
        var pot = [];
        IGNORETIP = 0;
        pot.push({
            text: "使用当前位置", onClick: function () {
                he_getlocation(setPoint);
            }
        });

            pot.push({
                text: "地图选点", onClick: function () {
                    he_getlocation(chooseMap);
                }
            });

        $.actions({actions: pot});
    });

    $('.confirm-popup').on('click', function () {
        $('#mapaddress').val(chooseMapRes.detail.address);
        setForm(chooseMapRes.detail.location.lat, chooseMapRes.detail.location.lng, 0);
    });

    function he_getlocation(callback) {
        if (HB_INWECHAT == 1) {
            wx.ready(function () {
                wx.getLocation({
                    type: 'gcj02',
                    success: function (res) {
//                        alert('微信定位');
                        callback(res);
                    },
                    cancel: function (res) {
                    }
                });
            });
        } else {
            var geolocation = new qq.maps.Geolocation(mkey, "myapp");
            geolocation.getLocation(callback, function () {
            }, {timeout: 4000, failTipFlag: true});
        }
    }

</script>
<script>
    if ($('#openlocation').length > 0) {
//        setTimeout(function () {
//            IGNORETIP = 1;
//            he_getlocation(setPoint);
//        }, 800);
    }
    if (typeof wx !== 'undefined') {
//        wx.ready(function () {
//            if ($('#openlocation').length > 0) {
//                he_getlocation(setPoint);
//            }
//        });
    }

//    $.alert('暂不支持IOS小程序入驻商家', function () {
//        wx.miniProgram.redirectTo({url: '/pages/pub/index'});
//        setTimeout(function () {
//            window.history.go(-1);
//        }, 600);
//    });
</script>